package com.redoor.app.stake.basicdata.service.impl;


import com.redoor.app.stake.basicdata.model.BaseStake;
import com.redoor.app.stake.basicdata.model.constants.BasicDataRedisConstants;
import com.redoor.app.stake.basicdata.service.BaseStakeRedisService;
import com.redoor.app.stake.common.service.RedisService;
import com.yotouch.app.stake.model.Stake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * Created by wangjunlei on 2018/1/30.
 */
@Service
public class BaseStakeRedisServiceImpl implements BaseStakeRedisService {

    @Autowired
    RedisService redisService;

    @Override
    public boolean modifyStakenInfo(String uuid, BaseStake stakeInfo) {
        try{
            if(StringUtils.isEmpty(stakeInfo.getUuid())
                    || StringUtils.isEmpty(uuid)
                    || !stakeInfo.getUuid().equals(uuid)){
                return false;
            }

            String realKey = BasicDataRedisConstants.REDIS_STAKE_PRE+uuid;
            String stakeNoKey = BasicDataRedisConstants.REDIS_STAKE_PRE+stakeInfo.getStakeno();
            String registrationUuidKey = BasicDataRedisConstants.REDIS_STAKE_PRE+stakeInfo.getRegistrationuuid();

            redisService.add(realKey,stakeInfo,BasicDataRedisConstants.REDIS_OVER_TIME_HOUR * 4);//桩实体设置四小时后过期
            redisService.add(realKey+":REGISTRATIONUUID",stakeInfo.getRegistrationuuid(),BasicDataRedisConstants.REDIS_OVER_TIME_HOUR * 4);//注册码主键 4小时过期
            redisService.add(realKey+":AUTHCODE",stakeInfo.getAuthcode(),BasicDataRedisConstants.REDIS_OVER_TIME_HOUR * 4);//授权码主键 4小时过期
            redisService.add(realKey+":STAKENO",stakeInfo.getStakeno(),BasicDataRedisConstants.REDIS_OVER_TIME_HOUR * 4);//桩编号 4小时过期
            redisService.add(realKey+":OUTSTAKENO",stakeInfo.getOutstakeno(),BasicDataRedisConstants.REDIS_OVER_TIME_HOUR * 4);//提供给互联互通的编号 4小时过期

            redisService.add(stakeNoKey+":UUID",uuid,BasicDataRedisConstants.REDIS_OVER_TIME_HOUR * 4);//添加StakeNo key键 4小时过期
            redisService.add(registrationUuidKey+":UUID",uuid,BasicDataRedisConstants.REDIS_OVER_TIME_HOUR * 4);//添加RegistrationUuid key键 4小时过期

            //根据桩表的注册码主键查找授权码，前置验证时需要
            redisService.add(registrationUuidKey+":AUTHCODE",stakeInfo.getAuthcode(),BasicDataRedisConstants.REDIS_OVER_TIME_HOUR * 12);

        } catch (Exception ex) {
            return false;
        }
        return true;
    }

    @Override
    public boolean deleteStakeInfo(String uuid) {
        String realKey = BasicDataRedisConstants.REDIS_STAKE_PRE+uuid+"*";
        BaseStake stake = getStakeInfoByUuid(BasicDataRedisConstants.REDIS_STAKE_PRE+uuid+":STAKENO");
        if (stake!=null){
            String stakeNoKey = BasicDataRedisConstants.REDIS_STAKE_PRE+stake.getStakeno()+"*";
            redisService.deleteByPattern(stakeNoKey); // 删除 前缀+[stakeNo]+* 规则的缓存
        }
        redisService.deleteByPattern(realKey);

        return true;
    }

    @Override
    public boolean isExistsStake(String uuid) {
        String realKey = BasicDataRedisConstants.REDIS_STAKE_PRE+uuid;
        return redisService.isExists(realKey);
    }

    @Override
    public boolean isExistsStakeByNo(String stakeNo) {
        String realKey = BasicDataRedisConstants.REDIS_STAKE_PRE+stakeNo;
        return redisService.isExists(realKey);
    }

    @Override
    public String getStakeUuidByNoHSF(String stakeNo) {
        String realKey = BasicDataRedisConstants.REDIS_STAKE_HSF_PRE+stakeNo+":UUID";
        return (String)redisService.get(realKey);
    }

    @Override
    public boolean isExistsStake(String uuid,String stakeNo) {
        String realKey = BasicDataRedisConstants.REDIS_STAKE_PRE+uuid+":STAKENO";
        return redisService.isExists(realKey);
    }

    @Override
    public BaseStake getStakeInfoByUuid(String uuid) {
        String realKey = BasicDataRedisConstants.REDIS_STAKE_PRE+uuid;
        return (BaseStake)redisService.get(realKey);
    }

    @Override
    public String getStakeInfoByNo(String stakeNo) {
        String realKey = BasicDataRedisConstants.REDIS_STAKE_PRE+stakeNo+":UUID";
        return (String)redisService.get(realKey);
    }

    @Override
    public String getStakeByReg(String regUuid) {
        String realKey = BasicDataRedisConstants.REDIS_STAKE_PRE+regUuid+":UUID";
        return (String)redisService.get(realKey);
    }

    @Override
    public String getAuthCodeByReg(String regUuid) {
        String key = BasicDataRedisConstants.REDIS_STAKE_PRE + regUuid + ":AUTHCODE";

        return (String)redisService.get(key);
    }

    private void modifyStakeOtherRedis(){
        String str = "";
    }


    //-----------------HSF接口---------------//


    @Override
    public boolean isExistsStakeHSF(String uuid) {
        String realKey = BasicDataRedisConstants.REDIS_STAKE_HSF_PRE+uuid;
        return redisService.isExists(realKey);
    }

    /**
     * 根据uuid获取stake对象--HSF接口使用
     * @param uuid
     * @return
     */
    @Override
    public Stake getStakeInfoByUuidHSF(String uuid) {
        String realKey = BasicDataRedisConstants.REDIS_STAKE_HSF_PRE+uuid;
        return (Stake) redisService.get(realKey);
    }

    /**
     * 更新Stake缓存数据，接口
     * @param uuid
     * @param stakeInfo
     * @return
     */
    @Override
    public boolean modifyStakeInfoHSF(String uuid, Stake stakeInfo) {
        try{
            if(StringUtils.isEmpty(stakeInfo.getUuid())
                    || StringUtils.isEmpty(uuid)
                    || !stakeInfo.getUuid().equals(uuid)){
                return false;
            }

            String realKey = BasicDataRedisConstants.REDIS_STAKE_HSF_PRE+uuid;
            String stakeNoKey = BasicDataRedisConstants.REDIS_STAKE_HSF_PRE+stakeInfo.getStakeNo();

            redisService.add(realKey,stakeInfo,BasicDataRedisConstants.REDIS_OVER_TIME_HOUR * 4);//桩实体设置四小时后过期
            redisService.add(realKey+":REGISTRATIONUUID",stakeInfo.getRegistration().getUuid(),BasicDataRedisConstants.REDIS_OVER_TIME_HOUR * 4);//注册码主键 4小时过期
            redisService.add(realKey+":AUTHCODE",stakeInfo.getAuthCode(),BasicDataRedisConstants.REDIS_OVER_TIME_HOUR * 4);//授权码主键 4小时过期
            redisService.add(realKey+":STAKENO",stakeInfo.getStakeNo(),BasicDataRedisConstants.REDIS_OVER_TIME_HOUR * 4);//桩编号 4小时过期
            redisService.add(realKey+":OUTSTAKENO",stakeInfo.getOutStakeNo(),BasicDataRedisConstants.REDIS_OVER_TIME_HOUR * 4);//提供给互联互通的编号 4小时过期

            redisService.add(stakeNoKey+":UUID",stakeInfo.getUuid(),BasicDataRedisConstants.REDIS_OVER_TIME_HOUR * 4);// 根据桩编号查找主键 4小时过期

        } catch (Exception ex) {
            return false;
        }
        return true;
    }
}
